package com.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

public class JedisUtils {
    private static JedisPool jp=null;
    private static String host=null;
    private static int port;
    private static int maxTotal;
    private static int maxIdle;

    static {//使用静态代码块，免得天天调用
//        加载配置
        ResourceBundle rb=ResourceBundle.getBundle("redis");
        host=rb.getString("redis.host");
        port=Integer.parseInt(rb.getString("redis.port"));
        maxTotal=Integer.parseInt(rb.getString("redis.maxTotal"));
        maxIdle=Integer.parseInt(rb.getString("redis.maxIdle"));
        //配置
        JedisPoolConfig jpc=new JedisPoolConfig();
        jpc.setMaxTotal(maxTotal);//最大连接数
        jpc.setMaxIdle(maxIdle);//最大活动数
        //使用连接池连接
        jp=new JedisPool(jpc,host,port);
    }

    public static Jedis getJedis(){
        return jp.getResource();
    }

    public static void main(String[] args) {
        JedisUtils.getJedis();
    }
}
